# -*- coding: UTF-8 -*-
"""
@author: tgy
@file: demo01.py
@time: 2021/02/23
"""


class Person:

    def __init__(self):
        self.name = 'lisi'

    # 当属性查找不到的时候会调用当前方法，可以最为装饰模式的外部容器
    def __getattr__(self, item):
        if item == 'age':
            return 21
        else:
            raise Exception('not allow attr')

    def __getattribute__(self, item):
        return super(Person, self).__getattribute__(item)


class Description:
    def __init__(self, label):
        self.label = label

    def __set__(self, instance, value):
        instance.__dict__[self.label] = value

    def __get__(self, instance, owner):
        return instance.__dict__[self.label]


class Dog:
    name = Description('name')

    def __init__(self, age):
        self._age = age

    @property
    def age(self):
        return self._age

    @age.setter
    def age(self, age):
        self._age = age


def test01():
    ...
    person = Person()
    # 对象.__class__ 等同于 type(对象)
    print(person.__class__)
    print(type(person))

    print(person.__dict__)
    print(dir(person))
    # vars(对象) == 对象.__dict__
    print(vars(person))


def test02():
    ...
    person = Person()
    print(person.age)


def test03():
    ...
    dog = Dog(21)
    dog.age = 22
    print(dog.age)
    dog.name = 'tony'
    print(dog.name)


if __name__ == '__main__':
    pass
    test03()
